﻿using CheckTicket.Sample.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Text;

namespace CheckTicket.Sample.Controllers
{
    /// <summary>
    ///
    /// </summary>
    [ApiController]
    [Route("")]
    public class TicketsController : ControllerBase
    {
        private readonly ILogger _logger;
        private readonly IMemoryCache _memoryCache;

        /// <summary>
        ///
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="memoryCache"></param>
        public TicketsController(ILogger<TicketsController> logger, IMemoryCache memoryCache)
        {
            _logger = logger;
            _memoryCache = memoryCache;
        }

        ///  <summary>
        /// 验票
        ///  </summary>
        ///  <param name="a"></param>
        ///  <param name="request"></param>
        ///  <param name="m"></param>
        ///  <returns></returns>
        [HttpPost]
        public IActionResult Checkticket([FromQuery] string m, [FromQuery] string a, [FromBody] object request)
        {
            MessageBus.Instance.Enqueue($"响应地址：?m={m}&a={a}");
            if (request == null) return Content(JsonConvert.SerializeObject(new GatealivecsResponse
            {
                status = "201",
                errorMessage = "请求体不能为空！",
            }, Formatting.None), "application/json;charset=GBK",
                Encoding.GetEncoding("GBK"));

            var content = request.ToString();
            if (string.IsNullOrWhiteSpace(content))
            {
                string json = JsonConvert.SerializeObject(new GatealivecsResponse
                {
                    status = "201",
                    errorMessage = "请求体不能为空！",
                }, Formatting.None);
                MessageBus.Instance.Enqueue($"响应内容：{json}");
                return Content(json, "application/json;charset=GBK",
                    Encoding.GetEncoding("GBK"));
            }

            MessageBus.Instance.Enqueue($"收到请求：{content}");
            if (string.IsNullOrWhiteSpace(m) || !"gateapi".Equals(m))
            {
                string json = JsonConvert.SerializeObject(new GatealivecsResponse
                {
                    status = "201",
                    errorMessage = "接口名无效！",
                }, Formatting.None);
                MessageBus.Instance.Enqueue($"响应内容：{json}");
                return Content(json, "application/json;charset=GBK",
                    Encoding.GetEncoding("GBK"));
            }

            if (string.IsNullOrWhiteSpace(a))
            {
                string json = JsonConvert.SerializeObject(new GatealivecsResponse
                {
                    status = "201",
                    errorMessage = "接口名无效！",
                }, Formatting.None);
                MessageBus.Instance.Enqueue($"响应内容：{json}");
                return Content(json, "application/json;charset=GBK",
                    Encoding.GetEncoding("GBK"));
            }

            var api = a.Trim().ToLower();
            switch (api)
            {
                case "gatealive":
                    //返回json数
                    if (!CommonSetting.Xtcg)
                    {
                        string json = JsonConvert.SerializeObject(new GatealivecsResponse
                        {
                            status = "201",
                            errorMessage = "失败！",
                        }, Formatting.None);
                        MessageBus.Instance.Enqueue($"响应内容：{json}");
                        return Content(json, "application/json;charset=GBK",
                            Encoding.GetEncoding("GBK"));
                    }
                    else
                    {
                        string json = JsonConvert.SerializeObject(new GatealivecsResponse
                        {
                            status = "200",
                            errorMessage = "成功！",
                        }, Formatting.None);
                        MessageBus.Instance.Enqueue($"响应内容：{json}");
                        return Content(json, "application/json;charset=GBK",
                            Encoding.GetEncoding("GBK"));
                    }
                case "checkticket":

                    //返回json数
                    if (!CommonSetting.Jpcg)
                    {
                        string json1 = JsonConvert.SerializeObject(new CheckticketResponse
                        {
                            checkResult = 0,
                            checkType = 0,
                            VoiceNum = CommonSetting.TicketType != null ? CommonSetting.TicketType.Value : 0,
                            checkMsg1 = "",
                            checkMsg2 = "",
                            checkMsg3 = "",
                            checkMsg4 = "验票失败"
                        }, Formatting.None);
                        MessageBus.Instance.Enqueue($"响应内容：{json1}");
                        return Content(json1, "application/json;charset=GBK",
                            Encoding.GetEncoding("GBK"));
                    }
                    else
                    {
                        string json1 = JsonConvert.SerializeObject(new CheckticketResponse
                        {
                            checkResult = 1,
                            checkType = 0,
                            VoiceNum = 1,
                            checkMsg1 = "票类：成人票",
                            checkMsg2 = "票号：000000000001",
                            checkMsg3 = "到期：2017-12-01",
                            checkMsg4 = "验票成功，请过闸"
                        }, Formatting.None);
                        MessageBus.Instance.Enqueue($"响应内容：{json1}");
                        return Content(json1, "application/json;charset=GBK",
                            Encoding.GetEncoding("GBK"));
                    }

                case "cancellation":
                    //返回json数
                    if (CommonSetting.Tpcg)
                    {
                        string json2 = JsonConvert.SerializeObject(new CancellationReponse
                        {
                            checkResult = "OK"
                        }, Formatting.None);
                        MessageBus.Instance.Enqueue($"响应内容：{json2}");
                        return Content(json2, "application/json;charset=GBK",
                            Encoding.GetEncoding("GBK"));
                    }
                    else
                    {
                        string json2 = JsonConvert.SerializeObject(new CancellationReponse
                        {
                            checkResult = "No"
                        }, Formatting.None);
                        MessageBus.Instance.Enqueue($"响应内容：{json2}");
                        return Content(json2, "application/json;charset=GBK",
                            Encoding.GetEncoding("GBK"));
                    }

                default:
                    string json3 = JsonConvert.SerializeObject(new CancellationReponse
                    {
                        checkResult = "OK"
                    }, Formatting.None);
                    MessageBus.Instance.Enqueue($"响应内容：{json3}");
                    return Content(json3, "application/json;charset=GBK",
                        Encoding.GetEncoding("GBK"));
            }
        }
    }
}